/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved.
 */

package com.device.idea.demo.view.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;

import com.device.idea.demo.R;
import com.device.idea.demo.constant.Constant;
import com.device.idea.demo.constant.ToolbarPosition;
import com.device.idea.demo.util.LogUtil;

/**
 * SettingPickerPopWindow
 * 设置选择弹出框
 *
 * @since 2020-12-22
 */
public class SettingPickerPopWindow extends BaseToolbarPopupWindow {
    private static final String TAG = SettingPickerPopWindow.class.getSimpleName();

    private OnWbSettingPickerListener mListener;

    private RadioGroup mRadioGroup;

    /**
     * 构造方法
     *
     * @param context 上下文对象
     * @param width popupWindow的宽
     * @param height popupWindow的高
     * @param isTouchable true-触摸popupWindow外部会消失
     */
    public SettingPickerPopWindow(@NonNull Context context, int width, int height, boolean isTouchable) {
        super(context, width, height, isTouchable);
        mContext = context;
    }

    @NonNull
    @Override
    protected View onCreateContentView(@NonNull Context context) {
        return createPopWindow(context);
    }

    @Override
    protected String getTag() {
        return TAG;
    }

    @Override
    public void show(@NonNull View anchorView, @NonNull ToolbarPosition toolbarPosition) {
        final int[] anchorLocation = new int[Constant.ANCHOR_LOCATION_ARRAY_SIZE]; // 存储X和Y坐标的数组
        anchorView.getLocationOnScreen(anchorLocation);
        final int screenWidth = anchorView.getResources().getDisplayMetrics().widthPixels;

        // 根据屏幕宽度和x轴的坐标，计算出x轴需要偏移的值
        super.showAtAnchorView(anchorView, toolbarPosition, screenWidth - anchorLocation[0], 0);
    }

    /**
     * OnWbBackgroundColorListener
     * 白板背景设置监听
     *
     * @since 2020-01-01
     */
    public interface OnWbSettingPickerListener {
        /**
         * 背景颜色变化通知
         *
         * @param color color
         */
        void onWbBackgroundColorChange(int color);
    }

    /**
     * setOnWbBackgroundColorListener
     *
     * @param listener OnWbSettingPickerListener
     */
    public void setOnWbBackgroundColorListener(OnWbSettingPickerListener listener) {
        mListener = listener;
    }

    private View createPopWindow(Context context) {
        View popWindowView = LayoutInflater.from(context).inflate(R.layout.pop_setting_picker_4k, null);
        mRadioGroup = popWindowView.findViewById(R.id.pop_setting_picker_canvas_select);

        mRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.pop_setting_picker_canvas_black) {
                onWbBackgroundColorSelect(Constant.CANVAS_BACKGROUND_COLOR_BLACK_RGBA);
            } else if (checkedId == R.id.pop_setting_picker_canvas_green) {
                onWbBackgroundColorSelect(Constant.CANVAS_BACKGROUND_COLOR_GREEN_RGBA);
            } else {
                onWbBackgroundColorSelect(Constant.CANVAS_BACKGROUND_COLOR_WHITE_RGBA);
            }
            dismiss();
        });

        int drawableWidth = context.getResources().getDimensionPixelSize(R.dimen.wb_setting_popwindow_item_width_4k);
        int drawableHeight = context.getResources().getDimensionPixelSize(R.dimen.wb_setting_popwindow_item_height_4k);

        Drawable initBlackDrawable = context.getResources().getDrawable(R.drawable.wb_tool_canvas_black, null);
        initBlackDrawable.setBounds(0, 0, drawableWidth, drawableHeight);
        RadioButton blackButton = popWindowView.findViewById(R.id.pop_setting_picker_canvas_black);
        blackButton.setCompoundDrawables(null, initBlackDrawable, null, null);

        Drawable initGreenDrawable = context.getResources().getDrawable(R.drawable.wb_tool_canvas_green, null);
        initGreenDrawable.setBounds(0, 0, drawableWidth, drawableHeight);
        RadioButton greenButton = popWindowView.findViewById(R.id.pop_setting_picker_canvas_green);
        greenButton.setCompoundDrawables(null, initGreenDrawable, null, null);

        Drawable initWhiteDrawable = context.getResources().getDrawable(R.drawable.wb_tool_canvas_white, null);
        initWhiteDrawable.setBounds(0, 0, drawableWidth, drawableHeight);
        RadioButton whiteButton = popWindowView.findViewById(R.id.pop_setting_picker_canvas_white);
        whiteButton.setCompoundDrawables(null, initWhiteDrawable, null, null);
        return popWindowView;
    }

    /**
     * updateChecked
     *
     * @param wbBackgroundColor background color
     */
    public void updateChecked(int wbBackgroundColor) {
        if (wbBackgroundColor == Constant.CANVAS_BACKGROUND_COLOR_WHITE_RGBA) {
            mRadioGroup.check(R.id.pop_setting_picker_canvas_white);
        } else if (wbBackgroundColor == Constant.CANVAS_BACKGROUND_COLOR_BLACK_RGBA) {
            mRadioGroup.check(R.id.pop_setting_picker_canvas_black);
        } else if (wbBackgroundColor == Constant.CANVAS_BACKGROUND_COLOR_GREEN_RGBA) {
            mRadioGroup.check(R.id.pop_setting_picker_canvas_green);
        } else {
            LogUtil.error(TAG, "illegal color,color:" + wbBackgroundColor);
        }
    }

    private void onWbBackgroundColorSelect(int color) {
        if (mListener != null) {
            mListener.onWbBackgroundColorChange(color);
        }
    }
}
